<!DOCTYPE html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>购物车</title>
    <script src="./js/vue.js"></script>
    <style>
        #content{
            margin: auto;
            width: 70%;
        }
        .Box{
            border: 1px solid #000;
            height: 45px;
            box-sizing: border-box;
            position: relative;
            border-radius: 5px;
        }
        .Box span{
            position: absolute;
            left: 1.5%;
            top: 20%;
        }
        #menu{
            border: 2px solid rgb(118, 230, 14);
            text-align: center;
            width: 100%;
            margin-top: 10px;
            border-radius: 10px;
        }
        #menu tr{
            height: 45px;
        }
        #menu tr>th{
            background-color: rgba(248, 204, 10, 0.922);
            font-size: 16px;
        }

    </style>
</head>

<body>
    <div id="app">
        <div id="content" >
            <div id="addTag" class="Box">
                <span>
                    <label>品牌名称</label>
                    <input type="text" v-model="goodName">
                    <button @click="addmenu">添加</button>
                    </span>         
            </div>
            <div id="searchTag" class="Box">
                <span>
                    <label>品牌名称</label>
                    <input type="text" v-model="findGoodName">
                    <button @click="getGoodList">搜索</button>
                </span>          
            </div>
            <table id="menu">
                <thead>
                    <tr>
                        <th>编号</th>
                        <th>商品名称</th>
                        <th>价格</th>
                        <th>数量</th>
                        <th>添加时间</th>
                        <th>操作</th>
                    </tr>

                </thead>
                <tbody>
                    <tr v-for="(value,index) in  goodList"  :key="index">
                        <td>{{index+1}}</td>
                        <td>{{value.name}}</td>
                        <td>{{value.yuan}}</td>
                        <td>
                            <button @click="handleReduce(index)">-</button>
                            {{value.num}}
                            <button @click="handleAdd(index)">+</button>
                        </td>
                        <td>{{value.time}}</td>
                        <td><a href="#" @click="delete1($event)">删除</a></td>
                    </tr>

                </tbody>
            </table>
            <div>价格总计是：{{totalPrice}}</div>
        </div>
    </div>
    <script>
        var tiMe = new Date().toLocaleString();
        var vm = new Vue({
            el: '#app',
            
            data: {
                goodList:[
                    {name:"欧美",yuan:50,num:10,time:tiMe},
                    {name:"日韩",yuan:50,num:10,time:tiMe},
                    {name:"大陆",yuan:50,num:10,time:tiMe},
                    {name:"港台",yuan:100,num:20,time:tiMe},

                ],
                goodName:'',
                findGoodName:''
            },
            methods: {
                addmenu(){
                    var goodObj = new Object();
                    goodObj.name = this.goodName;
                    var current_time = new Date().toLocaleString();
                    goodObj.time = current_time;
                    goodObj.yuan = 20;
                    goodObj.num = 1;
                    this.goodList.push(goodObj);

                },
                getGoodList(){
                    var tarGoodName = this.findGoodName;
                    var tempGoodList = new Array();
                    for(good of this.goodList){
                        if(good.name == tarGoodName){
                            tempGoodList.push(good)
                        }
                    }
                    this.goodList = tempGoodList;
                },
                handleReduce(index){
                    if(this.goodList[index].num == 1) return;
                    this.goodList[index].num--;

                },
                handleAdd(index){
                    this.goodList[index].num++;
                },
                delete1(event) {
                    if (confirm("是否删除?")) {
                        this.goodList.splice(event.data, 1)
                    }
                },
            },
            computed:{
                totalPrice(){
                    var total = 0;
                    for(var i=0;i<this.goodList.length;i++){
                        var item = this.goodList[i];
                        total += item.yuan*item.num;
                    }
                    return total;
                }
            } 
        })
    </script>
</body>

</html>